package com.order.web;

import com.demo.pojo.User;
import com.order.client.UserClient;
import com.order.pojo.OrderAndUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

// 刷新配置
@RefreshScope
@RestController
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserClient userClient;

    // 读取nacos中在配置文件内部自定义的一些配置的数据
    @Value("${aa.bb.cc}")
    private String test;

    @GetMapping("/get")
    public OrderAndUser getOrder(String id){
        System.out.println("test================="+test);
        System.out.println("order...id = " + id);
        OrderAndUser orderAndUser = new OrderAndUser();
        // 返回订单的信息，同时还需要包含订单对应的用户的信息
        orderAndUser.setId(id);
        orderAndUser.setTotalPrice(100);
        orderAndUser.setUserId("1");
        // 需要调用用户的服务，获取用户的数据
        ResponseEntity<User> forEntity = restTemplate.getForEntity("http://127.0.0.1:9001/" + id,User.class);
        User user = forEntity.getBody();
        orderAndUser.setUser(user);
        return orderAndUser;
    }
    @GetMapping("/get2")
    public OrderAndUser getOrder2(String id){
        System.out.println("......order.......id = " +id);
        // 返回订单的信息，同时还需要包含订单对应的用户的信息
        OrderAndUser orderAndUser = new OrderAndUser();
        orderAndUser.setId(id);
        orderAndUser.setTotalPrice(10000);
        orderAndUser.setUserId("1");
        // 使用OpenFeign调用user的服务
        User user = userClient.getUserById(id);
        orderAndUser.setUser(user);
        return orderAndUser;
    }
}